package com.dkd.model.auth.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dkd.model.auth.VO.UserVO;
import com.dkd.model.auth.entity.SysUser;
import com.dkd.model.auth.service.SysUserService;
import com.dkd.model.base.constants.PromptMessageEnum;
import com.dkd.model.base.pojo.ReturnData;
import com.dkd.model.base.utils.IdWorker;
import com.dkd.model.base.utils.LoggerUtil;
import com.dkd.model.base.utils.PageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

	/*生成主键工具*/
	@Value("${IdWorker.workerId}")
	private Integer workerId;
	@Value("${IdWorker.datacenterId}")
	private Integer datacenterId;
	private String getPK(){
		IdWorker idWorker = new IdWorker(workerId, datacenterId);
		Long id = idWorker.nextId();
		return id.toString();
	}

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 增加 Post Json
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/saveOrUpdate")
	public ReturnData<String> saveOrUpdate(@RequestBody SysUser sysUser){
		try{
			if(StringUtils.isBlank(sysUser.getId())){
				sysUser.setId(getPK());
			}
			//第一种方式(可增可更)
			sysUserService.saveOrUpdate(sysUser);
			//第二种方式(增)
			//sysUser.insert();
			//第二种方式(更)
			//sysUser.updateById();
			return new ReturnData<String>(PromptMessageEnum.SUCCEED,sysUser.getId());
		}catch (Exception e){
			LoggerUtil.setDebugLog(getClass(), "保存/更新用户失败");
			return new ReturnData<String>(PromptMessageEnum.FAILURE,e.getMessage());
		}
	}

	/**
	 * 删除
	 * @param sysUser
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public ReturnData<String> delete(@RequestBody SysUser sysUser){
		try{
			if(StringUtils.isBlank(sysUser.getId())){
				return new ReturnData<String>(PromptMessageEnum.FAILURE,"验证失败:ID is null");
			}else{
				sysUserService.removeById(sysUser);
				//sysUser.deleteById();
			}
			return new ReturnData<String>(PromptMessageEnum.SUCCEED,sysUser.getId());
		}catch (Exception e){
			LoggerUtil.setDebugLog(getClass(), "删除用户失败");
			return new ReturnData<String>(PromptMessageEnum.FAILURE,e.getMessage());
		}
	}

	/**
	 * 带分页-按查询条件查出数据列表
	 * http://localhost:8080/sysUser/getPageDataListByWrapper
	 */
	@RequestMapping(value = "/getPageDataListByWrapper")
	public ReturnData<IPage<SysUser>> getDataListByWrapperPage(@RequestBody UserVO userVO) {
		try{
			Wrapper wrapper = new QueryWrapper<SysUser>()
					.lambda().like(SysUser::getUsername, userVO.getUsername())
					.or(e -> e.like(SysUser::getPassword, userVO.getPassword()));
			IPage<SysUser> dataListPage = sysUserService.page(new Page<SysUser>(userVO.getCurrent(), userVO.getSize()), wrapper);
			ReturnData<IPage<SysUser>> rd = new ReturnData<IPage<SysUser>>(PromptMessageEnum.SUCCEED,"请求成功",dataListPage);
			return rd;
		}catch (Exception e){
			LoggerUtil.setDebugLog(getClass(), "查询用户失败");
			return new ReturnData<IPage<SysUser>>(PromptMessageEnum.FAILURE,e.getMessage());
		}
	}

	/**
	 * 带分页-用SQL语句查询数据列表  Mapper  查询
	 * 带分页-用SQL语句查询数据列表  @Select 标签查询
	 * http://localhost:8080/sysUser/selectPageListBySql
	 */
	@RequestMapping(value = "/selectPageListBySql")
	public ReturnData<PageUtil<SysUser>> selectPageListBySql(@RequestBody UserVO userVO) {
		try{
			PageUtil<SysUser> userListPage = sysUserService.selectPageListBySql(userVO);
			ReturnData<PageUtil<SysUser>> rd = new ReturnData<PageUtil<SysUser>>(PromptMessageEnum.SUCCEED,"请求成功", userListPage);
			return rd;
		}catch (Exception e){
			LoggerUtil.setDebugLog(getClass(), "查询用户失败");
			return new ReturnData<PageUtil<SysUser>>(PromptMessageEnum.FAILURE,e.getMessage());
		}
	}

	/**
	 * 用SQL语句查询数据列表
	 * http://localhost:8080/sysUser/selectListBySql
	 */
	@RequestMapping(value = "/selectListBySql")
	public ReturnData<List<SysUser>> selectListBySql(@RequestBody UserVO userVO) {
		try{
			List<SysUser> userListPage = sysUserService.selectListBySql(userVO);
			ReturnData<List<SysUser>> rd = new ReturnData<List<SysUser>>(PromptMessageEnum.SUCCEED,"请求成功", userListPage);
			return rd;
		}catch (Exception e){
			LoggerUtil.setDebugLog(getClass(), "查询用户失败");
			return new ReturnData<List<SysUser>>(PromptMessageEnum.FAILURE,e.getMessage());
		}
	}
}

